#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <EEPROM.h>
#include <uRTCLib.h>

// ========================================================================================================
// wifi
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <NTPClient.h>

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "ntp.aliyun.com", 8 * 60 * 60, 30 * 60 * 1000);
// Variables to save date and time
String formattedDate;
String dayStamp;
// ========================================================================================================

// --------------------------------------------------------------------------------------------------------
// These define's must be placed at the beginning before #include "ESP8266TimerInterrupt.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
#define _TIMERINTERRUPT_LOGLEVEL_ 1
//
// Select a Timer Clock
#define USING_TIM_DIV1 false   // for shortest and most accurate timer
#define USING_TIM_DIV16 false  // for medium time and medium accurate timer
#define USING_TIM_DIV256 true  // for longest timer but least accurate. Default
//
#include "ESP8266TimerInterrupt.h"
//
#define TIMER_INTERVAL_MS 50  // 50ms
//
// Init ESP8266 timer 1
ESP8266Timer ITimer;
// --------------------------------------------------------------------------------------------------------

// RTC
uRTCLib rtc(0x68);

// 默认地址 0x40
Adafruit_PWMServoDriver pwm0 = Adafruit_PWMServoDriver();
Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x41);

#define DELAY_SEC 100  // 延时
#define KEY_PRESS 2    // 按键防抖

char comchar;        // 串口字符命令
bool click = false;  // 是否按键
bool show_debug = true;
bool wifi_ok = true;  // wifi正常

// 准备微调设置第几个数字的第几个字段偏移量，h向上显示状态
uint8_t h0 = 0;

int ledPin = 2;  // LED 管脚

volatile bool readrtc = false;
volatile bool k_60 = false;  // 每分钟

volatile bool key_add = false;  // 按键加 14管脚
volatile bool key_set = false;  // 设置键 12管脚
volatile bool key_sub = false;  // 按键减 13管脚

#define STATE_DOWN 0                // 装配状态
#define STATE_SHOW 1                // 正常显示时间
#define STATE_HOUR 2                // 设置小时
#define STATE_MINU 3                // 设置分钟
#define STATE_SECO 4                // 设置10秒
#define STATE_SETT 5                // 设置4*7字段角度
volatile int mystate = STATE_DOWN;  // 开机装配向下，20s后自动恢复显示时间

#include "vari.h"       // 变量
#include "func.h"       // 函数
#include "setupfunc.h"  // Setup函数
#include "time.h"       // time函数

// 定时中断子程序，50ms
void IRAM_ATTR TimerHandler() {
  static int i, k, j, k12, k13, k14;
  static bool led;

  if (!digitalRead(14)) {
    k14++;
    if (k14 > KEY_PRESS) {
      key_add = true;
      k = 0;
    }
  } else {
    k14 = 0;
    key_add = false;
  }

  if (!digitalRead(13)) {
    k13++;
    if (k13 > KEY_PRESS) {
      key_sub = true;
      k = 0;
    }
  } else {
    k13 = 0;
    key_sub = false;
  }

  if (!digitalRead(12)) {
    k12++;
    if (k12 > KEY_PRESS) {
      key_set = true;
      k = 0;
    }
  } else {
    k12 = 0;
    key_set = false;
  }

  // 2s，显示LED灯，设置可以读取RTC
  i++;
  if (i >= 40) {
    i = 0;
    led = !led;  // 闪烁LED
    if (led) {
      digitalWrite(ledPin, HIGH);
    } else {
      digitalWrite(ledPin, LOW);
    };
    // 正常显示时间状态才可以读取RTC时间
    readrtc = mystate == STATE_SHOW;
  }

  // 20s，按键没有按下，超时，状态回到显示时间初始状态
  k++;
  if (k >= 400) {
    k = 0;
    if (mystate != STATE_SETT) {
      mystate = STATE_SHOW;
      readrtc = true;
      num0 = 0;  // 强制显示
      num1 = 0;
      num2 = 0;
      num3 = 0;
      if (false) {  // 光敏是否作用，黑天不再显示
        mystate = STATE_DOWN;
        readrtc = false;
        SetSEGDefault();
        Serial.println("Nithge coming ...");
      } else {
        Serial.println("Timeout backto STATE_SHOW ...");
      }
    }
  }

  // 60s
  j++;
  if (j >= 1200) {
    j = 0;
    k_60 = true;
  }
}

void setup() {
  Serial.begin(9600);
  Serial.println("My Clock by xtbanban(88185785@qq.com).");

  pinMode(14, INPUT_PULLUP);  // D5
  pinMode(12, INPUT_PULLUP);  // D6
  pinMode(13, INPUT_PULLUP);  // D7
  pinMode(ledPin, OUTPUT);    // LED灯

  pwm0.begin();
  pwm0.setPWMFreq(50);  // 50HZ更新频率，相当于20ms的周期
  pwm1.begin();
  pwm1.setPWMFreq(50);  // 50HZ更新频率，相当于20ms的周期

  EEPROM.begin(256);               //开启EEPROM，开辟空间
  SET_DEFAULT = EEPROM.read(255);  // 是否已经设置过默认值
  SetEeprom();

  URTCLIB_WIRE.begin(4, 5);  // ESP8266 - RTC

  if (!digitalRead(12)) {  // 如果开机按住K2，进入调整字段状态，所有数字显示8
    h0 = -1;
    mystate = STATE_SETT;
    // 刚开始全部上升
    Set0Seg0124(SEG_0_0, true);
    Set0Seg0124(SEG_0_1, true);
    Set0Seg0124(SEG_0_2, true);
    Set0Seg0124(SEG_0_4, true);
    Set0Seg356(true, true, true);
    delay(DELAY_SEC);
    Set1Seg0124(SEG_1_0, true);
    Set1Seg0124(SEG_1_1, true);
    Set1Seg0124(SEG_1_2, true);
    Set1Seg0124(SEG_1_4, true);
    Set1Seg356(true, true, true);
    delay(DELAY_SEC);
    Set2Seg0124(SEG_2_0, true);
    Set2Seg0124(SEG_2_1, true);
    Set2Seg0124(SEG_2_2, true);
    Set2Seg0124(SEG_2_4, true);
    Set2Seg356(true, true, true);
    delay(DELAY_SEC);
    Set3Seg0124(SEG_3_0, true);
    Set3Seg0124(SEG_3_1, true);
    Set3Seg0124(SEG_3_2, true);
    Set3Seg0124(SEG_3_4, true);
    Set3Seg356(true, true, true);
    delay(DELAY_SEC);
  } else {
    mystate = STATE_DOWN;
    SetSEGDefault();
  }

  ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler);

  WiFiManager wifiManager;
  if (!wifiManager.autoConnect("ESPAutoWiFi")) {
    // reset and try again, or maybe put it to deep sleep
    // ESP.reset();
    wifi_ok = false;
    Serial.println("WiFi Error!!!");
  } else {
    setrtctime();
    printtime();
  }
}

void loop() {
  show_debug = true;

  if (key_add) {
    key_add = false;
    Serial.println("key_add press...");
    if (mystate == STATE_SETT) {
      comchar = '<';
      click = true;
    } else if (mystate == STATE_HOUR) {
      hour++;
      if (hour > 24) {
        hour = 1;
      }
      delay(10 * DELAY_SEC);
    } else if (mystate == STATE_MINU) {
      minu++;
      if (minu > 59) {
        minu = 0;
      }
      delay(10 * DELAY_SEC);
    } else if (mystate == STATE_SECO) {
      seco_0++;
      if (seco_0 > 5 + 48) {
        seco_0 = 0 + 48;
      }
      delay(10 * DELAY_SEC);
    } else {
      mystate = STATE_SHOW;
    }
  }

  if (key_sub) {
    key_sub = false;
    Serial.println("key_sub press...");
    if (mystate == STATE_SETT) {
      comchar = '>';
      click = true;
    } else if (mystate == STATE_HOUR) {
      hour--;
      if (hour < 0) {
        hour = 24;
      }
      delay(10 * DELAY_SEC);
    } else if (mystate == STATE_MINU) {
      minu--;
      if (minu < 0) {
        minu = 59;
      }
      delay(10 * DELAY_SEC);
    } else if (mystate == STATE_SECO) {
      seco_0--;
      if (seco_0 < 0 + 48) {
        seco_0 = 5 + 48;
      }
      delay(10 * DELAY_SEC);
    } else {
      mystate = STATE_SHOW;
    }
  }

  if (key_set) {
    key_set = false;
    Serial.println("key2 set press...");
    if (mystate < STATE_SETT) {
      if (wifi_ok) {
        mystate = STATE_SHOW;  // wifi正常，不能设置了
      } else {
        mystate++;
      }
      if (mystate == STATE_HOUR) {
        Serial.println("set hour...");
        // SetHour();
        // delay(5 * DELAY_SEC);
        // num2 = 0;
        // num3 = 0;
        delay(10 * DELAY_SEC);
      } else if (mystate == STATE_MINU) {
        Serial.println("set minu...");
        SetMinu();
        delay(5 * DELAY_SEC);
        num0 = 0;
        num1 = 0;
      } else if (mystate == STATE_SECO) {
        Serial.println("set second...");
        SetSeco();
        delay(5 * DELAY_SEC);
        num0 = 0;
        num1 = 0;
      } else {
        mystate = STATE_SHOW;
        readrtc = true;
        num0 = 0;  // 强制显示
        num1 = 0;
        num2 = 0;
        num3 = 0;
        delay(10 * DELAY_SEC);
      }
    } else {
      mystate = STATE_SETT;
      comchar = 'h';
      click = true;
      show_debug = false;  // 不显示串口调试信息
    }
    Serial.print("mystate:");
    Serial.println(mystate);
  }

  if (readrtc && (mystate == STATE_SHOW)) {
    readrtc = false;
    // 取得rtc时间
    rtc.refresh();
    minu = rtc.minute();
    hour = rtc.hour();
    seco = rtc.second();
    //
    Serial.print("Read RTC:");
    Serial.print(hour, DEC);
    Serial.print(":");
    Serial.print(minu, DEC);
    Serial.println("");
  }

  if (k_60) {
    k_60 = false;
    // 1分钟了
    if (wifi_ok && mystate != STATE_SETT)  {
      setrtctime();
      printtime();
    }
  }

  if ((mystate > STATE_DOWN) && (mystate < STATE_SETT)) {
    // 如果时间有变化，则显示
    minu_0 = minu / 10 % 10 + 48;
    minu_1 = minu % 10 + 48;
    hour_0 = hour / 10 % 10 + 48;
    hour_1 = hour % 10 + 48;
    seco_0 - seco / 10 % 10 + 48;
    if (mystate == STATE_SECO) {
      minu_0 = seco_0;  // 调秒时，显示秒十位，个位显示0
      minu_1 = 0 + 48;
    }

    if (num1 != minu_1) {
      num1 = minu_1;
      set = 1;
      comchar = num1;
      click = true;
      delay(DELAY_SEC);
      Serial.println("show num1...");
    } else if (num0 != minu_0) {
      num0 = minu_0;
      set = 0;
      comchar = num0;
      click = true;
      delay(DELAY_SEC);
      Serial.println("show num0...");
    } else if (num3 != hour_1) {
      num3 = hour_1;
      set = 3;
      comchar = num3;
      click = true;
      delay(DELAY_SEC);
      Serial.println("show num3...");
    } else if (num2 != hour_0) {
      num2 = hour_0;
      set = 2;
      comchar = num2;
      click = true;
      delay(DELAY_SEC);
      Serial.println("show num2...");
    }
  }

  // 循环从串口读到数据，或者key置位
  while (Serial.available() > 0 || click) {
    if (click) {
      click = false;
    } else {
      comchar = Serial.read();  //读串口第一个字节
    }
    Serial.println(comchar);

    switch (comchar) {
      // 根据set变量，选择4位之一显示0到9
      case '0':
        SetSegnum0();
        break;
      case '1':
        SetSegnum1();
        break;
      case '2':
        SetSegnum2();
        break;
      case '3':
        SetSegnum3();
        break;
      case '4':
        SetSegnum4();
        break;
      case '5':
        SetSegnum5();
        break;
      case '6':
        SetSegnum6();
        break;
      case '7':
        SetSegnum7();
        break;
      case '8':
        SetSegnum8();
        break;
      case '9':
        SetSegnum9();
        break;
      case 'd':
        SetSegDown();
        break;

      // 所有字段到一半位置
      case 'm':
        SetSegHalf();
        break;

      // 字段向外一点
      case '<':
        switch (h0) {
          case 0:
            OFFSET_UP_0_0 = OFFSET_UP_0_0 + 10;
            break;
          case 1:
            OFFSET_UP_0_1 = OFFSET_UP_0_1 + 10;
            break;
          case 2:
            OFFSET_UP_0_2 = OFFSET_UP_0_2 - 10;
            break;
          case 3:
            OFFSET_UP_0_3 = OFFSET_UP_0_3 - 10;
            break;
          case 4:
            OFFSET_UP_0_4 = OFFSET_UP_0_4 + 10;
            break;
          case 5:
            OFFSET_UP_0_5 = OFFSET_UP_0_5 - 10;
            break;
          case 6:
            OFFSET_UP_0_6 = OFFSET_UP_0_6 + 10;
            break;
          // next seg
          case 0 + 7:
            OFFSET_UP_1_0 = OFFSET_UP_1_0 + 10;
            break;
          case 1 + 7:
            OFFSET_UP_1_1 = OFFSET_UP_1_1 + 10;
            break;
          case 2 + 7:
            OFFSET_UP_1_2 = OFFSET_UP_1_2 - 10;
            break;
          case 3 + 7:
            OFFSET_UP_1_3 = OFFSET_UP_1_3 - 10;
            break;
          case 4 + 7:
            OFFSET_UP_1_4 = OFFSET_UP_1_4 + 10;
            break;
          case 5 + 7:
            OFFSET_UP_1_5 = OFFSET_UP_1_5 - 10;
            break;
          case 6 + 7:
            OFFSET_UP_1_6 = OFFSET_UP_1_6 + 10;
            break;
          // next seg
          case 0 + 7 + 7:
            OFFSET_UP_2_0 = OFFSET_UP_2_0 + 10;
            break;
          case 1 + 7 + 7:
            OFFSET_UP_2_1 = OFFSET_UP_2_1 + 10;
            break;
          case 2 + 7 + 7:
            OFFSET_UP_2_2 = OFFSET_UP_2_2 - 10;
            break;
          case 3 + 7 + 7:
            OFFSET_UP_2_3 = OFFSET_UP_2_3 - 10;
            break;
          case 4 + 7 + 7:
            OFFSET_UP_2_4 = OFFSET_UP_2_4 + 10;
            break;
          case 5 + 7 + 7:
            OFFSET_UP_2_5 = OFFSET_UP_2_5 - 10;
            break;
          case 6 + 7 + 7:
            OFFSET_UP_2_6 = OFFSET_UP_2_6 + 10;
            break;
          // next seg
          case 0 + 7 + 7 + 7:
            OFFSET_UP_3_0 = OFFSET_UP_3_0 + 10;
            break;
          case 1 + 7 + 7 + 7:
            OFFSET_UP_3_1 = OFFSET_UP_3_1 + 10;
            break;
          case 2 + 7 + 7 + 7:
            OFFSET_UP_3_2 = OFFSET_UP_3_2 - 10;
            break;
          case 3 + 7 + 7 + 7:
            OFFSET_UP_3_3 = OFFSET_UP_3_3 - 10;
            break;
          case 4 + 7 + 7 + 7:
            OFFSET_UP_3_4 = OFFSET_UP_3_4 + 10;
            break;
          case 5 + 7 + 7 + 7:
            OFFSET_UP_3_5 = OFFSET_UP_3_5 - 10;
            break;
          case 6 + 7 + 7 + 7:
            OFFSET_UP_3_6 = OFFSET_UP_3_6 + 10;
            break;
        }
        SaveEeprom();
        if (h0 < 7) {
          set = 0;
        } else if (h0 < 7 + 7) {
          set = 1;
        } else if (h0 < 7 + 7 + 7) {
          set = 2;
        } else {
          set = 3;
        }
        comchar = '8';
        click = true;
        Serial.print("SAVE_SEGOFF:");
        Serial.println(h0);
        break;

      // 字段向内一点
      case '>':
        switch (h0) {
          case 0:
            OFFSET_UP_0_0 = OFFSET_UP_0_0 - 10;
            break;
          case 1:
            OFFSET_UP_0_1 = OFFSET_UP_0_1 - 10;
            break;
          case 2:
            OFFSET_UP_0_2 = OFFSET_UP_0_2 + 10;
            break;
          case 3:
            OFFSET_UP_0_3 = OFFSET_UP_0_3 + 10;
            break;
          case 4:
            OFFSET_UP_0_4 = OFFSET_UP_0_4 - 10;
            break;
          case 5:
            OFFSET_UP_0_5 = OFFSET_UP_0_5 + 10;
            break;
          case 6:
            OFFSET_UP_0_6 = OFFSET_UP_0_6 - 10;
            break;
          // next seg
          case 0 + 7:
            OFFSET_UP_1_0 = OFFSET_UP_1_0 - 10;
            break;
          case 1 + 7:
            OFFSET_UP_1_1 = OFFSET_UP_1_1 - 10;
            break;
          case 2 + 7:
            OFFSET_UP_1_2 = OFFSET_UP_1_2 + 10;
            break;
          case 3 + 7:
            OFFSET_UP_1_3 = OFFSET_UP_1_3 + 10;
            break;
          case 4 + 7:
            OFFSET_UP_1_4 = OFFSET_UP_1_4 - 10;
            break;
          case 5 + 7:
            OFFSET_UP_1_5 = OFFSET_UP_1_5 + 10;
            break;
          case 6 + 7:
            OFFSET_UP_1_6 = OFFSET_UP_1_6 - 10;
            break;
          // next seg
          case 0 + 7 + 7:
            OFFSET_UP_2_0 = OFFSET_UP_2_0 - 10;
            break;
          case 1 + 7 + 7:
            OFFSET_UP_2_1 = OFFSET_UP_2_1 - 10;
            break;
          case 2 + 7 + 7:
            OFFSET_UP_2_2 = OFFSET_UP_2_2 + 10;
            break;
          case 3 + 7 + 7:
            OFFSET_UP_2_3 = OFFSET_UP_2_3 + 10;
            break;
          case 4 + 7 + 7:
            OFFSET_UP_2_4 = OFFSET_UP_2_4 - 10;
            break;
          case 5 + 7 + 7:
            OFFSET_UP_2_5 = OFFSET_UP_2_5 + 10;
            break;
          case 6 + 7 + 7:
            OFFSET_UP_2_6 = OFFSET_UP_2_6 - 10;
            break;
          // next seg
          case 0 + 7 + 7 + 7:
            OFFSET_UP_3_0 = OFFSET_UP_3_0 - 10;
            break;
          case 1 + 7 + 7 + 7:
            OFFSET_UP_3_1 = OFFSET_UP_3_1 - 10;
            break;
          case 2 + 7 + 7 + 7:
            OFFSET_UP_3_2 = OFFSET_UP_3_2 + 10;
            break;
          case 3 + 7 + 7 + 7:
            OFFSET_UP_3_3 = OFFSET_UP_3_3 + 10;
            break;
          case 4 + 7 + 7 + 7:
            OFFSET_UP_3_4 = OFFSET_UP_3_4 - 10;
            break;
          case 5 + 7 + 7 + 7:
            OFFSET_UP_3_5 = OFFSET_UP_3_5 + 10;
            break;
          case 6 + 7 + 7 + 7:
            OFFSET_UP_3_6 = OFFSET_UP_3_6 - 10;
            break;
        }
        SaveEeprom();
        if (h0 < 7) {
          set = 0;
        } else if (h0 < 7 + 7) {
          set = 1;
        } else if (h0 < 7 + 7 + 7) {
          set = 2;
        } else {
          set = 3;
        }
        comchar = '8';
        click = true;
        Serial.print("SAVE_SEGOFF:");
        Serial.println(h0);
        break;

      // 向上显示状态下，选择需要设置位置的字段
      case 'h':
        mystate = STATE_SETT;  // 强制进入设置状态
        // 下一个字段
        h0++;
        if (h0 > 6 + 7 + 7 + 7) {
          h0 = -1;  // 设置结束了，继续设置
          Serial.print("setting again...");
          break;
        } else {
          Serial.print("SET_SEGOFF:");
          Serial.println(h0);
        }
        // 要设置的字段摇一下
        if (h0 == 0) {
          pwm0.setPWM(SEG_0_0, 0, SERVO_135_A);
        } else if (h0 == 1) {
          pwm0.setPWM(SEG_0_1, 0, SERVO_135_A);
        } else if (h0 == 2) {
          pwm0.setPWM(SEG_0_2, 0, SERVO_135_R);
        } else if (h0 == 3) {
          pwm0.setPWM(SEG_0_3, 0, SERVO_135_R);
        } else if (h0 == 4) {
          pwm0.setPWM(SEG_0_4, 0, SERVO_135_R);
        } else if (h0 == 5) {
          pwm0.setPWM(SEG_0_5, 0, SERVO_135_A);
        } else if (h0 == 6) {
          pwm0.setPWM(SEG_0_3, 0, SERVO_90_R);
          pwm0.setPWM(SEG_0_5, 0, SERVO_90_A);
          delay(5 * DELAY_SEC);
          pwm0.setPWM(SEG_0_6, 0, SERVO_135_A);
          pwm0.setPWM(SEG_0_R, 0, SERVO_135_R);
          delay(5 * DELAY_SEC);
          pwm0.setPWM(SEG_0_6, 0, SERVO_180_A);
        } else if (h0 == 0 + 7) {
          // 下一个字了
          pwm0.setPWM(SEG_1_0, 0, SERVO_135_A);
        } else if (h0 == 1 + 7) {
          pwm0.setPWM(SEG_1_1, 0, SERVO_135_A);
        } else if (h0 == 2 + 7) {
          pwm0.setPWM(SEG_1_2, 0, SERVO_135_R);
        } else if (h0 == 3 + 7) {
          pwm0.setPWM(SEG_1_3, 0, SERVO_135_R);
        } else if (h0 == 4 + 7) {
          pwm0.setPWM(SEG_1_4, 0, SERVO_135_R);
        } else if (h0 == 5 + 7) {
          pwm0.setPWM(SEG_1_5, 0, SERVO_135_A);
        } else if (h0 == 6 + 7) {
          pwm0.setPWM(SEG_1_3, 0, SERVO_90_R);
          pwm0.setPWM(SEG_1_5, 0, SERVO_90_A);
          delay(5 * DELAY_SEC);
          pwm0.setPWM(SEG_1_6, 0, SERVO_135_A);
          pwm0.setPWM(SEG_1_R, 0, SERVO_135_R);
          delay(5 * DELAY_SEC);
          pwm0.setPWM(SEG_1_6, 0, SERVO_180_A);
        } else if (h0 == 0 + 7 + 7) {
          // 再下一个字了
          pwm1.setPWM(SEG_2_0, 0, SERVO_135_A);
        } else if (h0 == 1 + 7 + 7) {
          pwm1.setPWM(SEG_2_1, 0, SERVO_135_A);
        } else if (h0 == 2 + 7 + 7) {
          pwm1.setPWM(2, 0, SERVO_135_R);
        } else if (h0 == 3 + 7 + 7) {
          pwm1.setPWM(SEG_2_3, 0, SERVO_135_R);
        } else if (h0 == 4 + 7 + 7) {
          pwm1.setPWM(SEG_2_4, 0, SERVO_135_R);
        } else if (h0 == 5 + 7 + 7) {
          pwm1.setPWM(SEG_2_5, 0, SERVO_135_A);
        } else if (h0 == 6 + 7 + 7) {
          pwm1.setPWM(SEG_2_3, 0, SERVO_90_R);
          pwm1.setPWM(SEG_2_5, 0, SERVO_90_A);
          delay(5 * DELAY_SEC);
          pwm1.setPWM(SEG_2_6, 0, SERVO_135_A);
          pwm1.setPWM(SEG_2_R, 0, SERVO_135_R);
          delay(5 * DELAY_SEC);
          pwm1.setPWM(SEG_2_6, 0, SERVO_180_A);
        } else if (h0 == 0 + 7 + 7 + 7) {
          // 再下一个字了
          pwm1.setPWM(SEG_3_0, 0, SERVO_135_A);
        } else if (h0 == 1 + 7 + 7 + 7) {
          pwm1.setPWM(SEG_3_1, 0, SERVO_135_A);
        } else if (h0 == 2 + 7 + 7 + 7) {
          pwm1.setPWM(2, 0, SERVO_135_R);
        } else if (h0 == 3 + 7 + 7 + 7) {
          pwm1.setPWM(SEG_3_3, 0, SERVO_135_R);
        } else if (h0 == 4 + 7 + 7 + 7) {
          pwm1.setPWM(SEG_3_4, 0, SERVO_135_R);
        } else if (h0 == 5 + 7 + 7 + 7) {
          pwm1.setPWM(SEG_3_5, 0, SERVO_135_A);
        } else if (h0 == 6 + 7 + 7 + 7) {
          pwm1.setPWM(SEG_3_3, 0, SERVO_90_R);
          pwm1.setPWM(SEG_3_5, 0, SERVO_90_A);
          delay(5 * DELAY_SEC);
          pwm1.setPWM(SEG_3_6, 0, SERVO_135_A);
          pwm1.setPWM(SEG_3_R, 0, SERVO_135_R);
          delay(5 * DELAY_SEC);
          pwm1.setPWM(SEG_3_6, 0, SERVO_180_A);
        }
        // 延时长一点
        delay(5 * DELAY_SEC);
        // 再全部上升
        Set0Seg0124(SEG_0_0, true);
        Set0Seg0124(SEG_0_1, true);
        Set0Seg0124(SEG_0_2, true);
        Set0Seg0124(SEG_0_4, true);
        Set0Seg356(true, true, true);
        delay(DELAY_SEC);
        Set1Seg0124(SEG_1_0, true);
        Set1Seg0124(SEG_1_1, true);
        Set1Seg0124(SEG_1_2, true);
        Set1Seg0124(SEG_1_4, true);
        Set1Seg356(true, true, true);
        delay(DELAY_SEC);
        Set2Seg0124(SEG_2_0, true);
        Set2Seg0124(SEG_2_1, true);
        Set2Seg0124(SEG_2_2, true);
        Set2Seg0124(SEG_2_4, true);
        Set2Seg356(true, true, true);
        delay(DELAY_SEC);
        Set3Seg0124(SEG_3_0, true);
        Set3Seg0124(SEG_3_1, true);
        Set3Seg0124(SEG_3_2, true);
        Set3Seg0124(SEG_3_4, true);
        Set3Seg356(true, true, true);
        delay(DELAY_SEC);
        break;
      default:
        break;

      case 'z':  // 偏移默认值全部重新设置为默认
        SET_DEFAULT = 0;
        SetEeprom();
        Serial.println("set eeprom default...");
        break;

      case 'g':  // 天黑了，全部下降
        mystate = STATE_DOWN;
        readrtc = false;
        SetSEGDefault();
        Serial.println("in nithge...");
        break;

      case 's':  // 模拟进入设置状态
        if (mystate == STATE_SETT) {
          num0 = 0;
          num1 = 0;
          num2 = 0;
          num3 = 0;
          mystate = STATE_SHOW;
        } else {
          h0 = -1;
          comchar = 'h';
          click = true;
        }
        break;

      case 'q':  // 模拟按键：add
        key_add = true;
        Serial.println("key_add press...");
        break;

      case 'w':  // 模拟按键：set
        key_set = true;
        Serial.println("key_set press...");
        break;

      case 'e':  // 模拟按键：sub
        key_sub = true;
        Serial.println("key_sub press...");
        break;
    }

    if (show_debug) {
      show_debug = true;
      // 显示参数mystate
      Serial.print("mystate:");
      Serial.print(mystate, DEC);
      Serial.println(" ");
      Serial.println("---------------------");

      Serial.print("num0:");
      Serial.print(num0 - 48);
      Serial.println(" ");

      Serial.print("OFF:");
      Serial.print(OFFSET_UP_0_0, DEC);
      Serial.print(" ");
      Serial.print(OFFSET_UP_0_1, DEC);
      Serial.print(" ");
      Serial.print(OFFSET_UP_0_2, DEC);
      Serial.print(" ");
      Serial.print(OFFSET_UP_0_3, DEC);
      Serial.print(" ");
      Serial.print(OFFSET_UP_0_4, DEC);
      Serial.print(" ");
      Serial.print(OFFSET_UP_0_5, DEC);
      Serial.print(" ");
      Serial.print(OFFSET_UP_0_6, DEC);
      Serial.print(" ");
      Serial.print(OFFSET_UP_0_6, DEC);
      Serial.println(" ");

      Serial.print("GET:");
      Serial.print(pwm0.getPWM(SEG_0_0, true), DEC);
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_0_1, true), DEC);
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_0_2, true), DEC);
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_0_3, true), DEC);
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_0_4, true), DEC);
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_0_5, true), DEC);
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_0_6, true), DEC);
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_0_R, true), DEC);
      Serial.println(" ");

      Serial.print("U&D:");
      Serial.print(pwm0.getPWM(SEG_0_0, true) > SERVO_90_A ? "up" : "down");
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_0_1, true) > SERVO_90_A ? "up" : "down");
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_0_2, true) < SERVO_90_R ? "up" : "down");
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_0_3, true) < SERVO_90_R ? "up" : "down");
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_0_4, true) < SERVO_90_R ? "up" : "down");
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_0_5, true) > SERVO_90_A ? "up" : "down");
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_0_6, true) > SERVO_90_A ? "up" : "down");
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_0_R, true) > SERVO_90_R ? "up" : "down");
      Serial.println(" ");

      Serial.print("num1:");
      Serial.print(num1 - 48);
      Serial.println(" ");

      Serial.print("OFF:");
      Serial.print(OFFSET_UP_1_0, DEC);
      Serial.print(" ");
      Serial.print(OFFSET_UP_1_1, DEC);
      Serial.print(" ");
      Serial.print(OFFSET_UP_1_2, DEC);
      Serial.print(" ");
      Serial.print(OFFSET_UP_1_3, DEC);
      Serial.print(" ");
      Serial.print(OFFSET_UP_1_4, DEC);
      Serial.print(" ");
      Serial.print(OFFSET_UP_1_5, DEC);
      Serial.print(" ");
      Serial.print(OFFSET_UP_1_6, DEC);
      Serial.print(" ");
      Serial.print(OFFSET_UP_1_6, DEC);
      Serial.println(" ");

      Serial.print("GET:");
      Serial.print(pwm0.getPWM(SEG_1_0, true), DEC);
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_1_1, true), DEC);
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_1_2, true), DEC);
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_1_3, true), DEC);
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_1_4, true), DEC);
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_1_5, true), DEC);
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_1_6, true), DEC);
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_1_R, true), DEC);
      Serial.println(" ");

      Serial.print("U&D:");
      Serial.print(pwm0.getPWM(SEG_1_0, true) > SERVO_90_A ? "up" : "down");
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_1_1, true) > SERVO_90_A ? "up" : "down");
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_1_2, true) < SERVO_90_R ? "up" : "down");
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_1_3, true) < SERVO_90_R ? "up" : "down");
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_1_4, true) < SERVO_90_R ? "up" : "down");
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_1_5, true) > SERVO_90_A ? "up" : "down");
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_1_6, true) > SERVO_90_A ? "up" : "down");
      Serial.print(" ");
      Serial.print(pwm0.getPWM(SEG_1_R, true) > SERVO_90_R ? "up" : "down");
      Serial.println(" ");
      Serial.println("---------------------");
    }
  }
}
